<?php

// IP信息
namespace app\utils;

use think\Cache;
use think\Request;

class IpInfoUtils
{
    /**
     * IP信息
     *
     * @param string $ip IP地址
     *
     * @return array
     */
    public static function info($ip = '')
    {
        return self::ip2region($ip);
        if (empty($ip)) {
            try {
                $ip = Request::ip();
            } catch (\Exception $e) {
                return false;
            }
        }
        $key = 'f3281ea00d3eeedf201892297a651085';
        $ip_key  = 'ip_info_2:' . $ip;
        $ip_info = Cache::get($ip_key);
        if (empty($ip_info)) {
            $url = 'https://restapi.amap.com/v3/ip?ip=' . $ip . '&output=json&key='.$key;
            $ip_info = http_get($url);
            if (empty($ip_info)) {
                $par['ip']        = $ip;
                $ip_info = http_post($url, $par);
            }

            if ($ip_info && $ip_info['province']) {
                $ip_ttl = 7 * 24 * 60 * 60;
                Cache::set($ip_key, $ip_info, $ip_ttl);
            }
        }

        return $ip_info;
    }
    public static function infoold($ip = '')
    {
        return self::ip2region($ip);
        if (empty($ip)) {
            try {
                $ip = Request::ip();
            } catch (\Exception $e) {
                return false;
            }
        }

        $ip_key  = 'ip_info:' . $ip;
        $ip_info = Cache::get($ip_key);
        if (empty($ip_info)) {
            $url = 'http://ip.taobao.com/outGetIpInfo?ip=' . $ip . '&accessKey=alibaba-inc';
            $res = http_get($url);
            if (empty($res)) {
                $par['ip']        = $ip;
                $par['accessKey'] = 'alibaba-inc';
                $res = http_post($url, $par);
            }

            $ip_info = [
                'ip'       => $ip,
                'country'  => '',
                'province' => '',
                'city'     => '',
                'area'     => '',
                'region'   => '',
                'isp'      => '',
            ];

            if ($res) {
                if ($res['code'] == 0 && $res['data']) {
                    $data = $res['data'];

                    $country  = $data['country'];
                    $province = $data['region'];
                    $city     = $data['city'];
                    $area     = $data['area'];
                    $region   = $country . $province . $city . $area;
                    $isp      = $data['isp'];

                    $ip_info['ip']       = $ip;
                    $ip_info['country']  = $country;
                    $ip_info['province'] = $province;
                    $ip_info['city']     = $city;
                    $ip_info['region']   = $region;
                    $ip_info['area']     = $area;
                    $ip_info['isp']      = $isp;

                    $ip_ttl = 7 * 24 * 60 * 60;
                    Cache::set($ip_key, $ip_info, $ip_ttl);
                }
            }
        }

        return $ip_info;
    }

    public static function infobaidu($ip = '')
    {
        return self::ip2region($ip);
        if (empty($ip)) {
            try {
                $ip = Request::ip();
            } catch (\Exception $e) {
                return false;
            }
        }

        $ip_key  = 'ip_info:' . $ip;
        $ip_info = Cache::get($ip_key);
        // $ip_info = Cache::tag('ipinfo')->get($ip_key);
        if (empty($ip_info)) {
            // 此处填写你在控制台-应用管理-创建应用后获取的AK
            $ak = 'Qs9nDzKBDgpUGVhqGGzFnVElLNB4hvjl';
            

            // 请求地址
            $url = 'https://api.map.baidu.com/location/ip';
            
            // 构造请求参数
            $param['ip']   = $ip;
            $param['coor']   = 'bd09ll';
            $param['ak']   = $ak;

            $res = request_get($url, $param);

            $result = json_decode($res, true);
            // 算作海外ip
            $ip_info['ip']       = $ip;
            if ($result && isset($result['status']) && $result['status'] != 302 && ($result['status'] == 1 || !isset($result['content']) || !$result['content']['address'])) {
                $ip_info['country']  = '';
                $ip_info['province'] = 'XX';
                $ip_info['city']     = 'XX';
                $ip_info['region']   = 'XX';
                $ip_info['area']     = 'XX';
                $ip_info['isp']      = 'XX';
            } else {
                $ip_info['country']  = '中国';
                $ip_info['province'] = $result['content']['address_detail']['province']??'';
                $ip_info['city']     = $result['content']['address_detail']['city']??'';
                $ip_info['region']   = '';
                $ip_info['area']     = '';
                $ip_info['isp']      = '';
                $ip_info['point']     = $result['content']['point']??'';
            }

            if ($result && $result['status'] == 0) {
                $ip_ttl = 30 * 24 * 60 * 60;
                Cache::set($ip_key, $ip_info, $ip_ttl);
                Cache::tag('ipinfo')->set($ip_key, $ip_info, $ip_ttl);
            }
        }
        return $ip_info;
    }


    public static function ip2region($ip = '')
    {
        if (empty($ip)) {
            try {
                $ip = Request::ip();
            } catch (\Exception $e) {
                return false;
            }
        }

        $ip_key  = 'ip_info:' . $ip;
        $ip_info = Cache::get($ip_key);
        if (empty($ip_info)) {
            try {
                $ip2region = new \Ip2Region();
                $result = $ip2region->memorySearch($ip);
                $result = explode('|', $result['region']);
                $ip_info = [
                    'ip'       => $ip,
                    'country'  => $result[0],
                    'province' => $result[2],
                    'city'     => $result[3],
                    'area'     => '',
                    'region'   => '',
                    'isp'      => $result[4],
                ];
                $ip_ttl = 30 * 24 * 60 * 60;
                Cache::set($ip_key, $ip_info, $ip_ttl);
                Cache::tag('ipinfo')->set($ip_key, $ip_info, $ip_ttl);
            } catch (\Exception $e) {
                return false;
            }
            // echo "<pre>";
            // var_dump($ip_info);
        }
        return $ip_info;
    }
}
